$(function(){
	if($("#sydwState").val()==1||$("#sydwState").val()==2) {
		$("#firstName").attr("readonly","readonly");
	}
	
	$("#assetsSource").change(function(){
		if($("#assetsSource").val()=="其他"){
			$("#assetsSourceAdd").css("display","");
			$('#assetsSourceAdd').validatebox({ 
				required:true
			}); 
		} else {
			$("#assetsSourceAdd").val("");
			$("#assetsSourceAdd").css("display","none");
			$('#assetsSourceAdd').validatebox({ 
				required:false
			});
		}
	});
	
	if($("#assetsSource").val() == "其他"){
		$("#assetsSourceAdd").css("display","");
	}
/*	$("#assetSum").keyup(function (){
		$("#showAsDw").html($.trim($(this).val())!=""?$(this).val()+"万元":"");
	}).blur(function (){
		$("#showAsDw").html($.trim($(this).val())!=""?$(this).val()+"万元":"");
	}).focus(function (){
		$("#showAsDw").html($.trim($(this).val())!=""?$(this).val()+"万元":"");
	});*/
	
	$('.td_link_but#qitaNameId').click(function(){
		add_input();
	});
	$('.td_link_but#qitaJbdwId').click(function(){
		add_input2();
	});
	$(':radio[name="applyInfo.receiveWay"]').click(function(){
		selectAddress($(this).val());
	});
	var ckWay=$(':radio[name="applyInfo.receiveWay"]:checked').val();
	selectAddress(ckWay);
	setNumberBox();
	setName();
	setName2();
	$(".datebox :text").attr("readonly","readonly");
	
	if ($("#spjgType").val() == 1) {
		$("#auditTr1,#auditTr2").css("display","none");
	}
	$("#spjgType").change(function(){
		if ($("#spjgType").val() == 1) {
			$("#auditTr1,#auditTr2").css("display","none");
		} else {
			$("#auditTr1,#auditTr2").css("display","");
		}
	});
});

function getRootPath() {
	var localObj = window.location;
	var contextPath = localObj.pathname.split("/")[1];
	return localObj.protocol + "//" + localObj.host + "/" + contextPath;
}

function ajaxValidFirstName(path) {
	var flag = true;
	$.ajax({
		type : "post",
		url : path + "/sldj/valiFirstName.do",
		cache : false,
		async : false,
		data : {firstName : $("#firstName").val(),sydwId : $("#sydwId").val()},
		success : function(data) {
			if (data != null) {
				flag = validFirstName(data);
			}
		},
		error : function() {
			alert("网络出现异常");
			flag = false;
			return false;
		}
	});
	return flag;
}

function validFirstName(obj) {
	var flag = true;
	var valid = obj.success;
	if (valid == 0) {
		flag = false;
		$.messager.alert('提示', '单位名称(第一名称)在系统中存在！', 'error', function() {
			$("#firstName").focus();
			$('#firstName').select();
		});
	} else if (valid == 1) {
		if(!confirm("你单位法人证书已被废止，现在是否重新办理设立登记？")) {
			flag = false;
		}
	} else if (valid == 2) {
		if (!confirm($("#firstName").val() + "与已注销的单位名称相同，是否继续使用？")) {
			flag = false;
		}
	} else if (valid == 990) {
		flag = false;
		$.messager.alert('提示', '单位正在提交申请事项！', 'error', function() {
			$("#firstName").focus();
			$('#firstName').select();
		});
	} else if (valid == 991) {
		flag = false;
		$.messager.alert('提示', '单位已经提交申请事项，事证号：'+obj.applyCode+'！', 'error', function() {
			$("#firstName").focus();
			$('#firstName').select();
		});
	}
	return flag;
}

function ajaxReferPage1(path){
	var isAfreshSldj = false;
	if($("#sydwState").val()==1||$("#sydwState").val()==2) {
		isAfreshSldj = true;
	}
	var val=true;
	$.ajax({
		type:'post',
		url:path+"/sldj/valiReferPage1.do",
		cache : false,
		async : false,
		data : {isAfreshSldj : isAfreshSldj},
		success:function(mst){
			if(mst!=null&&mst.success){
				val=false;
			}else{
				val=true;
			}
		},
		error:function(){
			alert('网络出现异常');
			val= false;
		}
	});
	return val;
}
function add_input(){
	if($('.qtname').length<5){
		var html=''+
			'<tr class="qtname">'+
			'<th width="16%">'+
	 		'<span class="bbname"></span>'+
	 	 	'</th>'+
	 	 	'<td width="84%" colspan="5" align="left">'+
	 	 	'<input type="text" class="input_text_400" name="dwNames" maxlength="400"/>'+
	 	 	//'<!--<a href="###" class="td_link_but" onclick="remove_input(this)">删除</a>-->'+
	 	 	'</td>'+
	 	 	'</tr>';
		$(html).insertAfter('.qtname:last');
		setName();
	}else{
		alert("没有可添加的单位名称！");
	}
}
function add_input2(){
	if($('.qtname2').length<5){
		var html=''+
			'<tr class="qtname2">'+
			'<th width="16%">'+
	 		'<span class="bbname2"></span>'+
	 	 	'</th>'+
	 	 	'<td width="84%" colspan="5" align="left">'+
	 	 	'<input type="text" class="input_text_240" name="dwNames2" maxlength="400"/>'+
	 	 	//'<!--<a href="###" class="td_link_but" onclick="remove_input2(this)">删除</a>-->'+
	 	 	'</td>'+
	 	 	'</tr>';
		$(html).insertAfter('.qtname2:last');
		setName2();
	}else{
		alert("没有可添加的举办单位！");
	}
}
function remove_inputs(){
	if($('.qtname').length>1){
		$('.qtname:last').remove();
		setName();
	}else{
		alert("没有可删除的单位名称！");
	}
}
function remove_input2s(){
	if($('.qtname2').length>1){
		$('.qtname2:last').remove();
	}else{
		alert("没有可删除的举办单位！");
	}
}
function remove_input(obj){
	$(obj).parentsUntil(".qtname").parent().remove();
	setName();
}
function remove_input2(obj){
	$(obj).parentsUntil(".qtname2").parent().remove();
}
function setName(){
	$('.bbname').each(function(index){
		if(index==0){
			$(this).html("单位名称(第二名称)");
		}else if(index==1){
			$(this).html("单位名称(第三名称)");
		}else if(index==2){
			$(this).html("单位名称(第四名称)");
		}else if(index==3){
			$(this).html("单位名称(其他名称)");
		}
	});
}
function setName2(){
	$('.bbname2').each(function(index){
		if(index==0){
			$(this).html("举办单位(二)");
		}else if(index==1){
			$(this).html("举办单位(三)");
		}else if(index==2){
			$(this).html("举办单位(四)");
		}else if(index==3){
			$(this).html("举办单位(五)");
		}
	});
}

function showTable(condition, objId) {
	if (condition) {
		$("#"+objId).show();
	}
}

function selectAddress(val){
	if(0==val){
		$('#kdAddress').show();
		$("#kdAddress input").removeAttr("disabled");
		$("#kdAddress input").validatebox({required:true});
	}else{
		$('#kdAddress').hide();
		$("#kdAddress input").attr("disabled","disabled");
		$("#kdAddress input").validatebox({required:false});
	}
}
function setNumberBox(){
	$(':input[name="gdInstion.assetsSum"]').numberbox({precision:2});
	$(':input[name="gdInstion.authStaff"]').numberbox({min:0});
	$(':input[name="gdInstion.authInfact"]').numberbox({min:0});
	$(':input[name="gdInstion.practNum"]').numberbox({min:0});
	$(':input[name="gdInstion.postPhone"]').numberbox({min:0});
	$(':input[name="gdInstion.postCode"]').numberbox({min:0});
}
function disabled_table(){
	$("#kdAddress input").attr("disabled","disabled");
	$("#refer-mode input").attr("disabled","disabled");
	$("#refer-mode textarea").attr("disabled","disabled");
	$("#refer-mode select").attr("disabled","disabled");
	$("#qtname_span").hide();
	$("#qtname2_span").hide();
}
function enabled_table(){
	$("#kdAddress input").removeAttr("disabled");
	$("#refer-mode input").removeAttr("disabled");
	$("#refer-mode textarea").removeAttr("disabled");
	$("#refer-mode select").removeAttr("disabled");
	$("#qtname_span").show();
	$("#qtname2_span").show();
}
function validPostCode() {
	var ckWay=$(':radio[name="applyInfo.receiveWay"]:checked').val();
	if(ckWay == "0"){
		if(!validNumLength("#postCode",6)) {
			$.messager.alert('提示','请输入6位数字的邮政编码','',function(){
				$('#postCode').select();
			});
			return false;
		}
	}
	return true;
}
function validateTable(path){
	var isValid = $("#rf_fom1").form('validate');
	if (isValid) {
		if(!validPostCode()) {
			isValid = false;
		} else if ($("#applyId").val() == 0 && !ajaxReferPage1(path)) {
			$.messager.alert('提示', '用户已经申请了表格！');
			isValid = false;
		} else if (!ajaxValidFirstName(path)) {
			isValid = false;
		} else if(!valiDwName()){
			isValid = false;
		} else if(!valiDwNames2()) {
			isValid = false;
		}
	}
	return isValid;
}

function validInputTable(path){
	var isValid = $("#rf_fom1").form('validate');
	if (isValid) {
		if(!validPostCode()) {
			isValid = false;
		} else if (!ajaxValidFirstName(path)) {
			isValid = false;
		} else if(!valiDwName()) {
			isValid = false;
		} else if(!valiDwNames2()) {
			isValid = false;
		}
	}
	return isValid;
}

function viewApplyTable(state) {
	disabled_table();
	if ($("#applyState").val() > state) {
		$("select,input,textarea").attr("disabled", "disabled");
		$("#approveDate").datebox('disable');
		$("#editBtnId,#saveBtnId").css("display","none");
	}
}

function editApplyTable() {
	$("#editBtnId").click(function() {
		enabled_table();
		$("#isEdit").val(1);
	});
}

function viewHylbCategory() {
	$("#industryCode").click(function(){
		init();
		$("#cate_form").window("open");
	});
	$("#closeForm").click(function(){
		$("#cate_form").window("close");
	});
}

function valiDwName(){
	var bn=true;
	$(':input[name="dwNames"]').each(function(ind){
		var dwName=$.trim($(this).val());
		if(dwName==""){
			alert("请填写单位名称！");
			$(this).select();
			bn=false;
			return false;
		}else if($("#firstName").val()==dwName){
			alert("单位名称不能跟第一单位名称相同！");
			bn=false;
			return false;
		}else{
			$(':input[name="dwNames"]').each(function(ind2){
				if(ind2>ind){
					var dwName2=$.trim($(this).val());
					if(dwName==dwName2){
						alert("单位名称不能重复！");
						bn=false;
						return false;
					}
				}
			});
			if(!bn){
				return false;
			}
		}
	});
	return bn;
}

function valiDwNames2(){
	var bn=true;
	$(':input[name="dwNames2"]').each(function(ind){
		var dwName=$.trim($(this).val());
		if(dwName==""){
			alert("请填写举办单位！");
			$(this).select();
			bn=false;
			return false;
		}else if($("#jbdwName").val()==dwName){
			alert("举办单位不能相同！");
			bn=false;
			return false;
		}else{
			$(':input[name="dwNames2"]').each(function(ind2){
				if(ind2>ind){
					var dwName2=$.trim($(this).val());
					if(dwName==dwName2){
						alert("举办单位不能重复！");
						bn=false;
						return false;
					}
				}
			});
			if(!bn){
				return false;
			}
		}
	});
	return bn;
}

function isExistJbdw(path) {
	var jbdwName = $.trim($(':input[id="jbdwName"]').val());
	$(':input[name="dwNames2"]').each(function(ind){
		jbdwName += "," + $.trim($(this).val());
	});
	var isFlag = true;
	jQuery.ajax({
		type : 'post',
		cache : false,
		async : false,
		url : path + '/isExistJbdw.do',
		data : {applyId : $("#applyId").val(),jbdwName : jbdwName},
		success : function(data) {
			if (!data.successful) {
				alert(data.message);
				isFlag = false;
			}
		},
		error : function() {
			alert('网络出现异常');
		}
	});
	return isFlag;
}

function saveApplyTable(path,actionName) {
	$("#saveBtnId").click(function(){
		if (validInputTable(path)) {
			isExistJbdw(path);
			if (confirm('确定要保存事业单位法人设立登记申请表吗?')) {
				try{parent.window.$.funkyUI({showDialog:false});}catch(e){$.funkyUI({showDialog:false});};
				$("#isSave").val(1);
				var d = new Date();
				var hours = ("0" + d.getHours()).slice(-2); // 获取当前小时数(0-23)
				var minutes = ("0" + d.getMinutes()).slice(-2); // 获取当前分钟数(0-59)
				var seconds = ("0" + d.getSeconds()).slice(-2); // 获取当前秒数(0-59)
				$("#hms").val(hours + ":" + minutes + ":" + seconds);
				frm.action = path + "/sldj/" + actionName + ".do";
				frm.submit();
				parent.window.$.unfunkyUI();
			}
		}
	});
}